﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsLU_EmailAddressTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// LU_EmailAddress Business Object
    /// </summary>

    public class LU_EmailAddress : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected LU_EmailAddress() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the LU_EmailAddress.
        /// </summary>
        public int LU_EmailAddressID
        {
            get { return _LU_EmailAddressID; }
            set
            {
                if (_LU_EmailAddressID != value)
                {
                    if (OnPropertyChanging("LU_EmailAddressID", _LU_EmailAddressID, value))
                    {
                        int oldValue = _LU_EmailAddressID;
                        _LU_EmailAddressID = value;

                        OnPropertyChanged("LU_EmailAddressID", oldValue, value);
                    }
                }
            }
        }  private int _LU_EmailAddressID = -1;

        /// <summary>
        /// EmailAddress.
        /// </summary>
        public string EmailAddress
        {
            get { return _emailaddress; }
            set
            {
                if (_emailaddress != value)
                {
                    if (OnPropertyChanging("EmailAddress", _emailaddress, value))
                    {
                        string oldValue = _emailaddress;
                        _emailaddress = value;
                        OnPropertyChanged("EmailAddress", oldValue, value);
                    }
                }
            }
        }  private string _emailaddress = null;


        /// <summary>
        /// Purpose.
        /// </summary>
        public string Purpose
        {
            get { return _purpose; }
            set
            {
                if (_purpose != value)
                {
                    if (OnPropertyChanging("Purpose", _purpose, value))
                    {
                        string oldValue = _purpose;
                        _purpose = value;
                        OnPropertyChanged("Purpose", oldValue, value);
                    }
                }
            }
        }  private string _purpose = null;



        /// <summary>
        /// UserType.
        /// </summary>
        public string UserType
        {
            get { return _usertype; }
            set
            {
                if (_usertype != value)
                {
                    if (OnPropertyChanging("UserType", _usertype, value))
                    {
                        string oldValue = _usertype;
                        _usertype = value;
                        OnPropertyChanged("UserType", oldValue, value);
                    }
                }
            }
        }  private string _usertype = null;


        /// <summary>
        /// Date/time at which the Email was used.
        /// </summary>
        public DateTime? RequestOn
        {
            get { return _requeston; }
            set
            {
                if (_requeston != value)
                {
                    if (OnPropertyChanging("RequestOn", _requeston, value))
                    {
                        DateTime? oldValue = _requeston;
                        _requeston = value;
                        OnPropertyChanged("RequestOn", oldValue, value);
                    }
                }
            }
        }  private DateTime? _requeston = null;


        /// <summary>
        /// True iff the Email was registered to the site(i.e. it is going to be used in the application).
        /// </summary>
        public bool IsRegistered
        {
            get { return _isRegistered; }
            set
            {
                if (_isRegistered != value)
                {
                    if (OnPropertyChanging("IsRegistered", _isRegistered, value))
                    {
                        bool oldValue = _isRegistered;
                        _isRegistered = value;
                        OnPropertyChanged("IsRegistered", oldValue, value);
                    }
                }
            }
        }  private bool _isRegistered = false;



        /// <summary>
        /// True iff the Admin has made this JobCategory Active (i.e. to be used in the application).
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                if (_isActive != value)
                {
                    if (OnPropertyChanging("IsActive", _isActive, value))
                    {
                        bool oldValue = _isActive;
                        _isActive = value;
                        OnPropertyChanged("IsActive", oldValue, value);
                    }
                }
            }
        }  private bool _isActive = false;


        /// <summary>
        /// True iff the Email was blocked by the administrator to the site(i.e. it is going not to be used in the application).
        /// </summary>
        public bool IsBlocked	
        {
            get { return _isBlocked; }
            set
            {
                if (_isBlocked != value)
                {
                    if (OnPropertyChanging("IsBlocked", _isBlocked, value))
                    {
                        bool oldValue = _isBlocked;
                        _isBlocked = value;
                        OnPropertyChanged("IsBlocked", oldValue, value);
                    }
                }
            }
        }  private bool _isBlocked = false;



        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty LU_EmailAddress object.
        /// </summary>
        public static LU_EmailAddress CreateNew()
        {
            LU_EmailAddress obj = new LU_EmailAddress();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.RequestOn = DateTime.Now;
            obj.IsRegistered = false;
            obj.IsBlocked = false;
            obj.IsActive = true;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static LU_EmailAddress CreateNew(LU_EmailAddressCollection collection)
        {
            LU_EmailAddress obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (LU_EmailAddress LU_EmailAddress in collection)
            {
                if (LU_EmailAddress.LU_EmailAddressID < minVal)
                    minVal = LU_EmailAddress.LU_EmailAddressID;
            }
            obj.LU_EmailAddressID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllLU_EmailAddress

        /// <summary>
        /// Returns a collection of all LU_EmailAddress.
        /// </summary>
        public static LU_EmailAddressCollection GetAllLU_EmailAddress()
        {
            LU_EmailAddressTableAdapter adapter = new LU_EmailAddressTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsLU_EmailAddress.LU_EmailAddressDataTable tbl = adapter.GetData(null);

            LU_EmailAddressCollection list = new LU_EmailAddressCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsLU_EmailAddress.LU_EmailAddressRow row in tbl.Rows)
                {
                    LU_EmailAddress LU_EmailAddress = new LU_EmailAddress();
                    RelationalToObject(LU_EmailAddress, row);
                    list.Add(LU_EmailAddress);
                }
            }

            return (list);
        }

        #endregion

        #region GetLU_EmailAddressByID

        /// <summary>
        /// Returns an LU_EmailAddress object for the specified 'LU_EmailAddressID' or null if no LU_EmailAddress with the specified parameter(s) exists.
        /// </summary>
        public static LU_EmailAddress GetLU_EmailAddressByID(int LU_EmailAddressid)
        {
            LU_EmailAddressTableAdapter adapter = new LU_EmailAddressTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsLU_EmailAddress.LU_EmailAddressDataTable tbl = adapter.GetData(LU_EmailAddressid);

            if (tbl.Rows.Count == 1)
            {
                LU_EmailAddress LU_EmailAddress = new LU_EmailAddress();
                RelationalToObject(LU_EmailAddress, tbl.Rows[0]);
                return (LU_EmailAddress);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'EmailAddressID' returned more than one User record.");

            return (null);
        }

        #endregion

        #region GetDataByEmailAddress

        /// <summary>
        /// Returns an LU_EmailAddress object for the specified 'EmailAddress' or null if no EmailAddress with the specified parameter(s) exists.
        /// </summary>
        public static LU_EmailAddress GetLU_EmailAddressByEmailAddress(string LU_EmailAddress)
        {
            LU_EmailAddressTableAdapter adapter = new LU_EmailAddressTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsLU_EmailAddress.LU_EmailAddressDataTable tbl = adapter.GetDataByEmailAddress(LU_EmailAddress);

            if (tbl.Rows.Count == 1)
            {
                LU_EmailAddress lu_emailaddress = new LU_EmailAddress();
                RelationalToObject(lu_emailaddress, tbl.Rows[0]);
                return (lu_emailaddress);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'EmailAddress' returned more than one User record.");

            return (null);
        }


        #endregion

        #region Update

        /// <summary>
        /// Updates the specified LU_EmailAddress.
        /// </summary>
        public static void Update(LU_EmailAddress obj)
        {
            bool updateLU_EmailAddress = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateLU_EmailAddress = (obj.ObjState != ObjectState.Unchanged);

                if (!updateLU_EmailAddress)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsLU_EmailAddress.LU_EmailAddressDataTable tbl = null;
            if (updateLU_EmailAddress)
            {
                LU_EmailAddressTableAdapter adapter = new LU_EmailAddressTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsLU_EmailAddress.LU_EmailAddressDataTable();
                dsLU_EmailAddress.LU_EmailAddressRow row = tbl.NewLU_EmailAddressRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (LU_EmailAddressID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("LU_EmailAddressID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((EmailAddress == null) || (EmailAddress.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("EmailAddress", "EmailAddress is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("LU_EmailAddress: LU_EmailAddressID={0}, EmailAddress={1}, {2}", LU_EmailAddressID, EmailAddress, Purpose));
        }

        #endregion

        #endregion

        #region Privates - None


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more LU_EmailAddress objects.
    /// </summary>

    public class LU_EmailAddressCollection : List<LU_EmailAddress>
    {
        #region Types

        public class UserComparer : IComparer<LU_EmailAddress>
        {
            public int Compare(LU_EmailAddress ea1, LU_EmailAddress ea2)
            {
                return (String.Compare(ea1.EmailAddress, ea1.EmailAddress));
            }
        }

        #endregion

        #region Public Methods

        #region FindByLU_EmailAddressID

        public LU_EmailAddress FindByLU_EmailAddressID(int LU_EmailAddressID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].LU_EmailAddressID == LU_EmailAddressID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindLU_EmailAddressByEmailAddress

        /// <summary>
        /// Case insensitve compare.
        /// </summary>
        public LU_EmailAddress FindLU_EmailAddressByEmailAddress(string LU_EmailAddress)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].EmailAddress == LU_EmailAddress)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
